<?php

namespace App\HttpController\Api;

use EasySwoole\Http\AbstractInterface\Controller;

class Base extends Controller
{

    public $params = [];

    public function index()
    {
    }

    public function onRequest(?string $action): ?bool
    {
        $this->getParams();
        // $this->writeJson(200,'账号无权限',[]);
        return true;
    }

    // public function onException(\Throwable $throwable): void
    // {
    //     $this->writeJson(404,'请求异常',[]);
    // }

    public function getParams()
    {
        $params = $this->request()->getRequestParam();
        $params['page'] = !empty($params['page']) ? intval($params['page']) : 1;
        $params['size'] = !empty($params['size']) ? intval($params['size']) : 5;
        $params['cat_id'] = !empty($params['cat_id']) ? intval($params['cat_id']) : 5;
        $params['from'] = ($params['page'] -1) * ($params['size']);
        $this->params = $params;
    }

    public function getPagingDatas($count, $data, $isSplice = 1)
    {
        $totalPage = ceil($count / $this->params['size']);
        if($totalPage > 100){
            $totalPage = 100;
        }
        $data = $data ?? [];
        $count = count($data);
        if($isSplice ==  1){
            $data = array_splice($data,$this->params['from'],$this->params['size']);
        }
        return [
            'total_page' => $totalPage,
            'page_size' => $this->params['page'],
            'count' => $count,
            'lists' => $data,
        ];
    }

}